﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnvDTE;
using System.IO;

namespace SymbolEditor.Addin.Helper
{
    class AddinHelper
    {
        public static string AddReference(EnvDTE.Project proj,string refPath)
        {
            string ret = string.Empty;
            VSLangProj.VSProject vsProject = (VSLangProj.VSProject)proj.Object;
            

            try
            {
                vsProject.References.Add(refPath);
            }
            catch (Exception ex)
            {
                ret = ex.ToString();
            }
            return ret;
        }

        public static ProjectItem checkDocumentExsist(EnvDTE.ProjectItems projItems, string filename)
        {
            ProjectItem ret = null;
            ProjectItem item = null;
            int foldercnt = 0;
            for (int i = 1; i <= projItems.Count; i++)
            {
                item = projItems.Item(i);
                if (item.Kind == Constants.vsProjectItemKindPhysicalFolder)
                {
                    if (item.ProjectItems.Count > 0)
                        foldercnt++;
                }
                else
                {
                    if (item.Kind == Constants.vsProjectItemKindPhysicalFile && item.Name.ToUpper().Equals(filename.ToUpper()))
                    {
                        ret = item;
                        break;
                    }
                }
            }
            if (ret == null)
            {
                if (foldercnt > 0)
                {
                    ProjectItem subItem = null;
                    for (int i = 1; i <= projItems.Count; i++)
                    {
                        item = projItems.Item(i);

                        if (item.Kind == Constants.vsProjectItemKindPhysicalFolder && item.ProjectItems.Count > 0)
                        {
                            subItem = checkDocumentExsist(item.ProjectItems, filename);
                            if (subItem != null)
                            {
                                ret = subItem;
                                break;
                            }
                        }
                    }
                }
            }
            // = findDocument(proj, filename);

            return ret;
        }

        public static ProjectItem AddEmptyDoc2Project(ProjectItems parent, string docname)
        {
            if ((new FileInfo(parent.ContainingProject.FullName)).IsReadOnly)
            {
                throw new FileLoadException("The project file '" + parent.ContainingProject.FullName + "' is read only!");
            }
            ProjectItem ret=null;
            string tmpDir = System.IO.Path.GetTempPath();
            string tmpCodefile = tmpDir + docname;
            if (File.Exists(tmpCodefile))
            {
                File.Delete(tmpCodefile);
            }
            FileInfo file = new FileInfo(tmpCodefile);
            FileStream stream = file.Create();
            stream.Dispose();


            ret = parent.AddFromFileCopy(tmpCodefile);

            File.Delete(tmpCodefile);
            return ret;
        }

        public delegate object safeDelegate(ProjectItem doc);

        public class SafeAccessDocument:IDisposable
        {
            ProjectItem doc = null;
            Window wnd = null;

            public SafeAccessDocument(ProjectItem doc)
            {
                this.doc = doc;
                if (doc.Kind == Constants.vsProjectItemKindPhysicalFile)
                {
                    if (!doc.IsOpen)
                    {
                        wnd = doc.Open(Constants.vsViewKindPrimary);
                    }
                }
            }

            #region IDisposable 成员

            public void Dispose()
            {
                if (wnd != null)
                    wnd.Close(vsSaveChanges.vsSaveChangesNo);

            }

            #endregion
        }

        //public static object SafeAccessDocument(ProjectItem doc, safeDelegate func = null)
        //{
        //    object ret = null;
        //    if (doc.Kind == Constants.vsProjectItemKindPhysicalFile)
        //    {
        //        if (!doc.IsOpen)
        //        {
        //            /*
        //            * the function 'libclsItem.Open' must be invoked
        //            * because visual studio initializes the object 'ProjectItem.Document'
        //            * asynchronously.So if call the function could force vs initializes
        //            * it right away.
        //            */
        //            Window wnd = doc.Open(Constants.vsViewKindPrimary);
        //            if (func != null)
        //            {
        //                ret = func(doc);
        //            }
        //            /*
        //            * if you open the document in the vs window,
        //            * of cource you should close it ,because of avoiding
        //            * the problem of opening the document later.
        //            */
        //            wnd.Close(vsSaveChanges.vsSaveChangesNo);
        //        }
        //        else
        //        {
        //            if (func != null)
        //            {
        //                ret = func(doc);
        //            }
        //        }

        //    }
        //    return ret;
        //}
    }
}
